package com.xinchao.ck_mips.view;

import android.Manifest;
import android.app.smdt.SmdtManager;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.xinchao.ck_mips.broadcast.HomeWatcherService;
import com.xinchao.ck_mips.utils.DeviceUtil;
import com.xinchao.ck_mips.utils.DisplayMetricsUtils;
import com.xinchao.ck_mips.utils.ShareUtil;

import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

/**
 * Created by wq on 2019/4/23.
 * 启动界面
 */
public class SplashActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (DeviceUtil.isLandscape(this)) {
            //横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        }

        DisplayMetricsUtils.setBackground(this);
        startService(new Intent(this, HomeWatcherService.class));
        requsetPermission();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Build.MODEL.contains("3280") || Build.MODEL.contains("IoT3288") || Build.MODEL.contains("DS835")) {
            //隐藏状态栏
            SmdtManager smt = SmdtManager.create(this);
            smt.smdtSetStatusBar(this, false);
        }
    }

    private void judge() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (TextUtils.isEmpty(ShareUtil.getInstance().getToken())) {
                    //未注册,跳转绑定界面，进行注册
                    startActivity(new Intent(SplashActivity.this, BindActivity.class));
                } else {
                    //播放界面
                    startActivity(new Intent(SplashActivity.this, PlayActivity.class));
                }
                finish();
            }
        }, 1000);
    }

    /**
     * 申请权限
     */
    private void requsetPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!EasyPermissions.hasPermissions(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                EasyPermissions.requestPermissions(this, "运行访问权限", 1001, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } else {
                judge();
            }
        } else {
            judge();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        //申请成功
        judge();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        //申请失败
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
    }
}
